package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateAddNewParam;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 处理属性模板相关请求的控制器
 */
@Slf4j
@RestController
@RequestMapping("/attribute-template")
@Api(tags = "06. 属性模板管理模块")
@Validated
public class AttributeTemplateController {

    @Autowired
    private IAttributeTemplateService attributeTemplateService;

    @ApiOperation("添加属性模板")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@Valid AttributeTemplateAddNewParam attributeTemplateAddNewParam) {
        log.debug("开始处理【添加属性模板】的请求，参数：{}", attributeTemplateAddNewParam);
        attributeTemplateService.addNew(attributeTemplateAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:^\\d+$}/delete")
    @ApiOperation("根据ID删除属性模板")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "long")
    public JsonResult delete(@Range(min = 1, message = "根据ID删除属性模板失败，请提交合法的ID值！") @PathVariable Long id) {
        log.debug("开始处理【根据ID删除属性模板】的请求，参数：{}", id);
        attributeTemplateService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:^\\d+$}/update")
    @ApiOperation("根据ID修改属性模板详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "long")
    public JsonResult update(@Range(min = 1, message = "根据ID修改属性模板详情失败，请提交合法的ID值！")
                             @PathVariable Long id, @Valid AttributeTemplateUpdateInfoParam attributeTemplateUpdateInfoParam) {
        log.debug("开始处理【根据ID修改属性模板】的请求，参数ID：{},参数：{}", id, attributeTemplateUpdateInfoParam);
        attributeTemplateService.updateInfoById(id, attributeTemplateUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/attribute-template/{id}
    @GetMapping("/{id:^\\d+$}")
    @ApiOperation("根据ID查询属性模板详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "long")
    public JsonResult getById(@Range(min = 1, message = "根据ID查询属性模板详情失败，请提交合法的ID值！") @PathVariable Long id) {
        log.debug("开始处理【根据ID查询属性模板】的请求，参数：{}", id);
        AttributeTemplateStandardVO attributeTemplate = attributeTemplateService.getById(id);
        return JsonResult.ok(attributeTemplate);
    }

    // http://localhost:9180/attribute-template?queryType=all
    @GetMapping("")
    @ApiOperation("查看属性模板列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(@Range(min = 1, message = "查看属性模板列表，请提供正确的页码值！") Integer page,String queryType) {
        log.debug("开始处理【查看属性模板列表】请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<AttributeTemplateListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = attributeTemplateService.list(1,Integer.MAX_VALUE);
        }else {
            pageData = attributeTemplateService.list(page);
        }

        return JsonResult.ok(pageData);
    }
}


